import { http } from "@/utils/http";
import { baseAdminURLApi, type PageResult } from "../utils";
import type { OperationQuery } from "./log";

// 分页查询用户
interface UserPageVO {
  id: number;
  username: string;
  nickname: string;
  mobile: string;
  gender: number;
  avatar: string;
  email: string;
  status: number;
  roleId: number;
  createTime: Date;
  remark: string;
}
export const getPageList = (data?: object) => {
  return http.request<PageResult<UserPageVO[]>>(
    "get",
    baseAdminURLApi("/users"),
    {
      params: data
    }
  );
};
// 用户Form
interface UserForm {
  id?: number;
  username: string;
  nickname: string;
  mobile: string;
  gender: number;
  email: string;
  status: number;
  roleId: number;
  remark: string;
}
// 新增用户
export const addUser = (data?: UserForm) => {
  return http.request<boolean>("post", baseAdminURLApi("/users"), {
    data
  });
};
// 修改用户信息
export const updateUser = (id: number, data?: UserForm) => {
  return http.request<boolean>("put", baseAdminURLApi(`/users/${id}`), {
    data
  });
};
// 修改用户状态
interface UserStatusDTO {
  id: number;
  status: number;
}
export const updateUserStatus = (data: UserStatusDTO) => {
  return http.request<boolean>("patch", baseAdminURLApi("/users/status"), {
    data
  });
};
// 根据id/ids删除用户  ids: 1,2,3,4
export const delUserById = (data?: Array<number>) => {
  return http.request<boolean>("delete", baseAdminURLApi("/users"), {
    data
  });
};
interface ResetPasswordDTO {
  id: number;
  password: string;
}
// 重置用户密码
export const resetPassword = (data: ResetPasswordDTO) => {
  return http.request<boolean>(
    "patch",
    baseAdminURLApi("/users/reset-password"),
    { data }
  );
};
// 上传头像
export const uploadAvatar = (userId: number, file?: File) => {
  // const formData = new FormData();
  // formData.append("avatar", file); // 后端用的是 MultipartFile avatar
  return http.upload(
    baseAdminURLApi(`/users/${userId}/avatar`),
    file,
    "avatar"
  );
};
// 获取当前用户信息
interface UserInfoVO {
  userId?: number;
  nickname: string;
  username: string;
  avatar: string;
  mobile: string | number;
  email: string;
  gender: string | number;
  roleName: string;
  content: string;
  createTime: Date;
}
export const getCurrentUser = () => {
  return http.request<UserInfoVO>("get", baseAdminURLApi("/users/profile/me"));
};
// 导出用户信息 Excel
export const exportUserInfoExcel = (fileName: string, data: OperationQuery) => {
  http.download(baseAdminURLApi("/users/export"), fileName, "导出", {
    params: { fileName }, // 作为 URL 参数
    data // 作为 JSON 请求体
  });
};
// 修改当前用户密码
export interface ChangePasswordForm {
  oldPassword: string;
  newPassword: string;
  confirmPassword: string;
}
export const updateCurrentUserPassword = (data: ChangePasswordForm) => {
  return http.request("patch", baseAdminURLApi("/users/profile/password"), {
    data
  });
};
// 修改当前用户信息
interface UserProfileForm {
  nickname: string;
  mobile: string;
  email: string;
  gender: number;
}
export const updateCurrentUserInfo = (data: UserProfileForm) => {
  return http.request("put", baseAdminURLApi("/users/profile"), {
    data
  });
};

/** 当前用户修改头像 */
export const uploadCurrentAvatar = (file?: File) => {
  return http.upload(baseAdminURLApi(`/users/profile/avatar`), file, "avatar");
};
